package com.ys.controller.web.finance;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.finance.TcwPaymentApply;
import com.ys.entity.finance.TcwPaymentApplyItem;
import com.ys.entity.flow.YspTaskAudit;
import com.ys.entity.flow.YspTaskAuditUser;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.flow.YspTaskAuditEnum;
import com.ys.enume.flow.YspTaskAuditUserEnum;
import com.ys.service.finance.TcwPaymentApplyService;
import com.ys.service.flow.YspTaskService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.Flow;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 付款申请单
 */
@Controller
@RequestMapping("/web/finance/tcwPaymentApply")
public class WebTcwPaymentApplyController {
	private static final Logger logger = LoggerFactory.getLogger(WebTcwPaymentApplyController.class);

	@Autowired
	private CommonApiService<TcwPaymentApply> commonApiService;

    @Autowired
    private YspTaskService yspTaskService;

    private Long menuId = MenuId.TCW_PaymentApply;

    @Autowired
    private TcwPaymentApplyService tcwPaymentApplyService;


    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> extraParam = new HashMap<>();
            //关联数据
            extraParam.put("status", 1);

            TcwPaymentApply entity = new TcwPaymentApply();
            //关联数据
            String bid = commonApiService.getCommonNextBid(menuId);
            entity.setBid(bid);

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuId,new ArrayList<>());
            return commonApiService.toAdd(model, entity, menuId, extraParam, subGridConfig);
        }catch (Exception e) {
            logger.error("新增页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
        try {
            Map<String, Object> extraParam = tcwPaymentApplyService.getExtraParam(bid);

            List<Map<String, Object>> itemList = tcwPaymentApplyService.getItemList(bid);
            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuId,itemList);

            //子表按钮
            //List<SubGridConfigDto.ButtonDto> buttonArr = subGridConfig.getButtonArr();
            Integer state = yspTaskService.flowState(bid);
            if (state == Flow.State.FINISH.value){
                subGridConfig.setButtonArr(new ArrayList<>());
            }else if (state == Flow.State.PROCESS.value){
                subGridConfig.setButtonArr(new ArrayList<>());
            }


            //在流转过程中，财务需要修改：付款开户行
            {
                YsProcInst ysProcInst = new YsProcInst().setBid(bid).queryFirst();
                int  isReSet  = 0;
                // 当前节点审核数据
                YspTaskAudit yspTaskAuditCurrent = new YspTaskAudit().setId(ysProcInst.getCurrentAuditId()).queryFirst();
                if (yspTaskAuditCurrent.getState() == YspTaskAuditEnum.State.FOR_AUDIT.value) {
                    YspTaskAuditUser yspTaskAuditUserCurrent = new YspTaskAuditUser()
                            .setTaskAuditId(yspTaskAuditCurrent.getId())
                            .setUserId(ShiroUtils.getUserId())
                            .queryFirst();
                    if (yspTaskAuditUserCurrent != null && ShiroUtils.getRoleName().contains("财务") ) {
                        isReSet  = 1;
                    }
                }
                model.addAttribute("isReSet",isReSet);
            }


            commonApiService.toEdit(model,Long.parseLong(extraParam.get("id").toString()), new TcwPaymentApply(), menuId, extraParam, subGridConfig);
            return "finance/tcwPaymentApply/toEdit";

        }catch (Exception e) {
            logger.error("编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 新增，根据导入数据
     */
    @GetMapping("/toAddByImport")
    public String toAddByImport(Model model, HttpServletRequest request, HttpServletResponse response, String bid){
        try {
            //主表信息
            Map<String, Object> extraParam = tcwPaymentApplyService.getExtraParam(bid);
            //子表信息
            List<Map<String, Object>> itemList = tcwPaymentApplyService.getItemList(bid);
            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuId,itemList);

            //是否校验 1是、0否
            extraParam.put("isCheck", 1);

            //子表按钮
            Integer state = yspTaskService.flowState(bid);
            if (state == Flow.State.FINISH.value){
                subGridConfig.setButtonArr(new ArrayList<>());
            }else if (state == Flow.State.PROCESS.value){
                subGridConfig.setButtonArr(new ArrayList<>());
            }

            TcwPaymentApply entity = new TcwPaymentApply()
                    .setBid(bid)
                    .setCreateTime(new Date());

            return commonApiService.toAdd(model,entity, menuId, extraParam, subGridConfig);

        } catch (Exception e) {
            e.printStackTrace();
            return W.error(model, e.getMessage());
        }
    }






}






